<?php
namespace Services;
/**
 * File holds class for feedback data manipulation
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Services
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Feedback extends ORM {
	/**
	 * Method parses input and then stores to database. 
	 * @param array $post HTML POST array
	 * @return \Entities\Feedback Feedback object on success, false on error
	 * @throws \Exception
	 */
	public function prepareInsert($post) {
		$feedback = false;
		if(isset($post) && count($post) > 0) {
		    $business = new \Entities\Business();
		    $business->setCode(\Misc\Tools::cleanInput($post['code']));
		    $business = $this->select($business);
			$feedback = new \Entities\Feedback();
			$feedback->setBusiness($business->getId());
			$feedback->setComment(\Misc\Tools::cleanInput($post['comment']));
			if (@$post['score'] == '-1') {
			    $score = null;
			} else {
			    $score = (int)$post['score'];
			}
			$feedback->setRating($score);
			$feedback->setInappropriate(\Enum\Feedback::Appropriate);
			//allow time override when input is from integration!
			if (isset($post['time'])) {
			    $time = strtotime($post['time']);
			} else {
			    $time = time();
			}
			$feedback->setTime($time);
			$feedback->setUser($post['uid']);
			
			$id = $this->insert($feedback);
			$feedback->setId($id);
		}
		return $feedback;
	}

	/**
	 * Method searches for comments by rater type and time
	 * @param int $detractors Flag whether to show detractors 
	 * @param int $passives Flag whether to show passives
	 * @param int $promoters Flag whether to show promoters
	 * @param var $from Date from 
	 * @param var $to Date to
	 * @param int $business Company ID
	 * @return array Array of feedbacks when found. False otherwise
	 * @throws \Exception
	 */
	public function commentSearch($detractors, $passives, $promoters, $from, $to, $business) {
		$out = false;
		$raters = array();
		if ($detractors > 0) {
			$raters[] = ' `rating` BETWEEN ' . \Enum\NPS::Detractors . ' AND ' . (\Enum\NPS::Passive - 1);
		}
		if ($passives > 0) {
			$raters[] = ' `rating` BETWEEN ' . \Enum\NPS::Passive . ' AND ' . (\Enum\NPS::Promoters - 1);
		}
		if ($promoters > 0) {
			$raters[] = ' `rating` BETWEEN ' . \Enum\NPS::Promoters . ' AND 10';
		}
		$sql = 'SELECT * FROM `feedback` where `business` = %d %s';
		$appendix = '';
		if (count($raters) > 0) {
			$appendix = ' AND (' . implode(' OR ', $raters) . ')';
		}
		$dates = '';
		if (strlen($from) > 0) {
		    $dates = sprintf(" AND `time` >= '%s' ", strtotime($from));
		} 
		//add 1 day to 'to' date, so that user can search comments for 1 day only:
		//etc: from 16.01.2012 to 16.01.2012 will show comments for that day
		if (strlen($to) > 0) {
		    $dates .= sprintf(" AND `time` <= '%s' ", strtotime('+1 day', strtotime($to)));
		}

		$preparedSql = sprintf($sql, $business, $dates);
		$preparedSql .= $appendix;
		$preparedSql .= ' ORDER BY `time` ASC';
		$out = $this->rawQuery($preparedSql, new \Entities\Feedback());

		return $out;
	}
	
	/**
	 * Method marks comment as inappropriate and updates DB
	 * @param int $id Feedback ID
	 * @return \Entities\Feedback inappropriate feedback object
	 * @throws \Exception when database transactions fail
	 */
	public function markInappropriate($id) {
	    $this->_logger->debug(sprintf('Marking comment %d as inappropriate!', $id));
		//Update feedback object
		$comTmpl = new \Entities\Feedback();
		$comTmpl->setId($id);
		$com = $this->select($comTmpl);
		$com->setInappropriate(\Enum\Feedback::Inappropriate);
		$this->update($com);
		
		return $com;
	}
}